package com.home.customtest.typehandle;

import com.custom.jdbc.handler.AbstractTypeHandler;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author XiaoBai
 * @since 2024/4/9 13:56
 */
public class StatusEnumTypeHandler extends AbstractTypeHandler<StatusEnum> {
    @Override
    public StatusEnum getTypeValue(Object val) {
        return StatusEnum.getByCode(((int) val));
    }

    @Override
    public StatusEnum getTypeValue(ResultSet rs, int index) throws SQLException {
        return getTypeValue(rs.getInt(index));
    }

    @Override
    public void setTypeValue(PreparedStatement statement, int index, StatusEnum val) throws SQLException {
        if (val != null) {
            statement.setInt(index, val.getCode());
        }else {
            statement.setNull(index, 0);
        }
    }
}
